﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Shoozla.ViewModel;
using Shoozla.View;
using System.Threading;
using Shoozla.DataModel;
using System.ComponentModel;

namespace Shoozla
{
    /// <summary>
    /// Interaction logic for FolderListView.xaml
    /// </summary>
    public partial class FolderListView : UserControl
    {
 
        public FolderListView()
        {
            InitializeComponent();
            FolderListViewModel folderList = new FolderListViewModel(System.Windows.Threading.Dispatcher.CurrentDispatcher);
            DataContext = folderList;
        }

       
        private void folderLV_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           App.MainView.tagEditor.SelectedSongChanged((Mp3File)folderLV.SelectedItem);
        }


        private void filterTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (App.AllItems == null)
                return;
            
            //display only song thats match the input string
            if (!String.IsNullOrEmpty(filterTB.Text))
            {
                String searchpattern = filterTB.Text.ToLower();
                var match = (
                        from m in App.AllItems
                        where m.Artist.ToLower().Contains(searchpattern) || m.Title.ToLower().Contains(searchpattern) || m.AlbumName.ToLower().Contains(searchpattern)
                        select m
                    );
                ((ViewModelBase)DataContext).CurrentItems.ReplaceCollection(match);
            }
            else
                ((ViewModelBase)DataContext).CurrentItems.ReplaceCollection(App.AllItems);
        }

        private void filterTB_GotFocus(object sender, RoutedEventArgs e)
        {
            filterTB.Text = String.Empty;
        }

        GridViewColumnHeader _lastHeaderClicked = null;
        ListSortDirection _lastDirection = ListSortDirection.Ascending;

        void GridViewColumnHeaderClickedHandler(object sender,RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;

            if (headerClicked != null)
            {
                if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                {
                    if (headerClicked != _lastHeaderClicked)
                    {
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (_lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }

                    string header = headerClicked.Column.Header as string;
                    Sort(header, direction);

                    _lastHeaderClicked = headerClicked;
                    _lastDirection = direction;
                }
            }
        }

        private void Sort(string sortBy, ListSortDirection direction)
        {
            if (sortBy == "Cover")
                return; //do not sort

            //album sort doesn't work

            ICollectionView dataView = CollectionViewSource.GetDefaultView(folderLV.ItemsSource);
            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }



        
    }
}
